﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：BidDrugList.cs
    // 文件功能描述：采购产品列表
    // 创建标识：吕海乐2009-03-19
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class BidDrugList : BasePage
    {
        public BidDrugList()
        {
            this.ModuleNo = Module.采购项目管理.ToString();
            this.PageOperateType = OperateType.基础产品列表.ToString();
        }

        IBLL_Goods ig = Factory.BLL_Goods();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
                if(CurrentAccount.GetUser().UserID.Substring(0,1).ToUpper() == "A")
                {
                    this.gvwBidDrug.Columns[14].Visible = false;
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            StoreQueryCondition();
            AspNetPager1.CurrentPageIndex = 1;
            //SetBind();
        }

        /// <summary>
        /// 绑定所有符合条件的采购产品
        /// </summary>
        private void SetBind()
        {
            HZ.DrugTrade.Model.Query.Goods goods = GetQueryCondition();
            if (goods == null)
            {
                StoreQueryCondition();
                goods = GetQueryCondition();
            }

            ////AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            gvwBidDrug.PageSize = AspNetPager1.PageSize;
            int totalCount = 0;
            IList<HZ.DrugTrade.Model.View.Goods> goodsList = ig.GetGoodsByWhere(goods, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwBidDrug.DataSource = goodsList;
            gvwBidDrug.DataBind();
        }

        /// <summary>
        /// 将查询条件存储到viewstate中
        /// </summary>
       private void StoreQueryCondition()
        {
            HZ.DrugTrade.Model.Query.Goods where = new HZ.DrugTrade.Model.Query.Goods();
            where.GoodsID = txtGoodsId.Text.Trim();
            where.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            where.PackUnit = txtPackUnit.Text.Trim();
            where.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            //if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            //{
            //    where.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            //}
            where.ProjectID = ddlProjectid.SelectedValue;
            where.QualityLevel = this.txtQualityLevel.Text.Trim().ToUpper();
            where.Outlookc = txtOutLook.Text.Trim();
            //where.Pack = txtPack.Text.Trim();
            where.Material = txtMaterial.Text.Trim();
            where.GoodsName = txtGoodsName.Text.Trim();
            where.Unit = txtUnit.Text.Trim();
            if (CurrentAccount.GetUser().UserID.Substring(0, 1).ToUpper() == "A")
            {
                where.Append = "  goodsid in (select goodsid from view_procurecatalog_tb where isusing =1)";
            }
            //if (ucSource1.SelectValue != -1) where.Source = ucSource1.SelectValue;
            //if (ucIsBasicRemedy1.SelectValue != -1) where.IsBasicRemedy = ucIsBasicRemedy1.SelectValue;

            where.SortFields = " GoodsName asc";

            ViewState["v_goods"] = where;
        }

        /// <summary>
        /// 讲查询条件从viewstate中取出
        /// </summary>
        private HZ.DrugTrade.Model.Query.Goods GetQueryCondition()
        {
            HZ.DrugTrade.Model.Query.Goods goods =(HZ.DrugTrade.Model.Query.Goods)ViewState["v_goods"];
            return goods;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
        }

        /// <summary>
        /// 简单查询与高级查询之间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtGoodsId.Text = "";
                //txtFactor.Text = "";
                txtQualityLevel.Text = "";
                txtPackUnit.Text = "";
                //txtPack.Text = "";
                txtProduceCompany_SC.Text = "";
                txtGoodsName.Text = "";
                txtUnit.Text = "";
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.Goods goods = GetQueryCondition();
            if (goods == null)
            {
                StoreQueryCondition();
                goods = GetQueryCondition();
            }
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.Goods>(goods, true);
            string sql = string.Format(@"SELECT Goodsid 产品编号,GoodsName 产品名称,Outlookc 规格型号,Material 材质,Unit 单位,
                                        PackUnit 包装单位,QualityLevel 产品类别,CompanyName_SC 生产企业,ProcurePrice 采购限价,case isusing when 1 then '启用' else '禁用' end as 是否启用 FROM View_ProcureCatalog_tb  WHERE 1=1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "基础产品库.xls");
        }

        protected void gvwBidDrug_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ddlProjectid.SelectedItem.Value == "0002")
                {
                    e.Row.Cells[12].Text = "自主报价";
                }
                else
                {
                    e.Row.Cells[12].Text = "采购限价";
                }
            }
        }
    }
}
